package com.xingongchang.baolebei.activity;

import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListViewTopic;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.SlideView;
import com.insthub.BeeFramework.view.SlideView.OnSlideListener;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;
import com.xingongchang.baolebei.protocol.PAGINATED;
import com.xingongchang.baolebei.protocol.USER_TOPIC;


public class TopicActivity extends BaseActivity implements BusinessResponse,OnSlideListener, 
	XListViewTopic.IXListViewListener {

	private ImageView backButton;
	private TextView title;
	private XListViewTopic xlistView;
	private boolean isSetAdapter = true;
	private UserInfoModel mUserInfoModel;
	private SlideAdapter mSlideAdapter;
	private SlideView mLastSlideViewWithStatusOn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_topic);
		findViewById();
		initView();
	}

	private void findViewById(){
		xlistView = (XListViewTopic) findViewById(R.id.list);
		backButton = (ImageView) findViewById(R.id.top_view_back);
		title = (TextView) findViewById(R.id.top_view_text);
	}

	private void initView() {
		backButton.setVisibility(View.VISIBLE);
		title.setText("我的帖子");
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				int topicId = mUserInfoModel.usertopicList.get(position - 1).topicid;
				Intent intent = new Intent(TopicActivity.this, TopicDetailActivity.class);
				intent.putExtra("topicid", topicId);
				startActivity(intent);
			}
		});
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setFooterDividersEnabled(false);
		xlistView.setXListViewListener(this, 1);
		mUserInfoModel = new UserInfoModel(this);
		mUserInfoModel.addResponseListener(this);
		mUserInfoModel.getUserTopic();
	}

	class SlideAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		ArrayList<USER_TOPIC> list;

		public SlideAdapter(ArrayList<USER_TOPIC> list) {
			mInflater = getLayoutInflater();
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			SlideView slideView = (SlideView) convertView;
			if (slideView == null) {
				View itemView = mInflater.inflate(R.layout.list_item, null);

				slideView = new SlideView(TopicActivity.this);
				slideView.setContentView(itemView);

				holder = new ViewHolder(slideView);
				slideView.setOnSlideListener(TopicActivity.this);
				slideView.setTag(holder);
			} else {
				holder = (ViewHolder) slideView.getTag();
			}
			USER_TOPIC topic = list.get(position);
			topic.slideView = slideView;
			topic.slideView.shrink();

			holder.t.setText(topic.title);
			holder.time.setText(getDateShort(topic.creaton));
			holder.deleteHolder.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mUserInfoModel.deleteTopic(list.get(position).topicid);
					list.remove(position);
					mSlideAdapter.notifyDataSetChanged();
				}
			});
			return slideView;
		}

	}

	private static class ViewHolder {
		public TextView t;
		public TextView time;
		public ViewGroup deleteHolder;

		ViewHolder(View view) {
			t = (TextView) view.findViewById(R.id.title);
			time = (TextView) view.findViewById(R.id.time);
			deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
		}
	}

	@Override
	public void onSlide(View view, int status) {
		if (mLastSlideViewWithStatusOn != null
				&& mLastSlideViewWithStatusOn != view) {
			mLastSlideViewWithStatusOn.shrink();
		}

		if (status == SLIDE_STATUS_ON) {
			mLastSlideViewWithStatusOn = (SlideView) view;
		}
	}

	public void setContent() {

		if (mSlideAdapter == null) {

			if (mUserInfoModel.usertopicList.size() != 0) {
				mSlideAdapter = new SlideAdapter(mUserInfoModel.usertopicList);
				xlistView.setAdapter(mSlideAdapter);
			}

		} else {

			if (isSetAdapter == true) {
				xlistView.setAdapter(mSlideAdapter);
			} else {
				mSlideAdapter.list = mUserInfoModel.usertopicList;
				mSlideAdapter.notifyDataSetChanged();
			}
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		// TODO Auto-generated method stub
		if (url.endsWith(ProtocolConst.USER_TOPIC)) {
			xlistView.stopRefresh();
			xlistView.stopLoadMore();
			xlistView.setRefreshTime();

			setContent();
			PAGINATED paginated = PAGINATED.fromJson(jo
					.optJSONObject("paginated"));
			if (0 == paginated.more) {
				xlistView.setPullLoadEnable(false);
			} else {
				xlistView.setPullLoadEnable(true);
			}
		}
	}

	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = true;
		mUserInfoModel.getUserTopic();
	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = false;
		mUserInfoModel.getUserTopicMore();
	}
}
